package com.yipei.business.controller;

import com.yipei.business.domain.MlAccountMessage;
import com.yipei.business.service.IMlAccountMessageService;
import com.yipei.common.annotation.Log;
import com.yipei.common.core.controller.BaseController;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.core.page.TableDataInfo;
import com.yipei.common.enums.BusinessType;
import com.yipei.common.utils.SecurityUtils;
import com.yipei.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 账户消息Controller
 *
 * @author yipei
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/business/accountmessage")
@Api(tags = "账号信息", description = "账号信息管理")
public class MlAccountMessageController extends BaseController {
    @Autowired
    private IMlAccountMessageService mlAccountMessageService;

    /**
     * 查询账户消息列表
     */
    @ApiOperation("账号信息获取数据")
//    @PreAuthorize("@loginMemberPermissionService.hasMePermi('business:accountmessage:list')")
    @GetMapping("/list")
    public TableDataInfo list(MlAccountMessage mlAccountMessage) {
        startPage();
        List<MlAccountMessage> list = mlAccountMessageService.selectMlAccountMessageList(mlAccountMessage);
        return getDataTable(list);
    }

    /**
     * 导出账户消息列表
     */
    @ApiOperation("账号信息到处数据")
    //@PreAuthorize("@ss.hasPermi('business:accountmessage:export')")
    @Log(title = "账户消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MlAccountMessage mlAccountMessage) {
        List<MlAccountMessage> list = mlAccountMessageService.selectMlAccountMessageList(mlAccountMessage);
        ExcelUtil<MlAccountMessage> util = new ExcelUtil<MlAccountMessage>(MlAccountMessage.class);
        util.exportExcel(response, list, "账户消息数据");
    }

    /**
     * 获取账户消息详细信息
     */
    @ApiOperation("账号信息通过ID获取数据")
    //@PreAuthorize("@ss.hasPermi('business:accountmessage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mlAccountMessageService.selectMlAccountMessageById(id));
    }

    /**
     * 新增账户消息
     */
    @ApiOperation("账号信息新增数据")
    //@PreAuthorize("@ss.hasPermi('business:accountmessage:add')")
    @Log(title = "账户消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MlAccountMessage mlAccountMessage) {
        return toAjax(mlAccountMessageService.insertMlAccountMessage(mlAccountMessage));
    }

    /**
     * 修改账户消息
     */
    @ApiOperation("账号信息修改数据")
    //@PreAuthorize("@ss.hasPermi('business:accountmessage:edit')")
    @Log(title = "账户消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MlAccountMessage mlAccountMessage) {
        return toAjax(mlAccountMessageService.updateMlAccountMessage(mlAccountMessage));
    }

    /**
     * 删除账户消息
     */
    @ApiOperation("账号信息删除数据")
    //@PreAuthorize("@ss.hasPermi('business:accountmessage:remove')")
    @Log(title = "账户消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mlAccountMessageService.deleteMlAccountMessageByIds(ids));
    }

    @ApiOperation("个人账号获取消息")
//    @PreAuthorize("@loginMemberPermissionService.hasMePermi('business:accountmessage:list')")
    @GetMapping("/tdAccList")
    public AjaxResult accountMessage(MlAccountMessage mlAccountMessage) {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        List<MlAccountMessage> list = mlAccountMessageService.lambdaQuery().eq(MlAccountMessage::getTdAcc, tdAcc).list();
//        mlAccountMessageService.lambdaUpdate().eq(MlAccountMessage::getTdAcc,tdAcc).set(MlAccountMessage::getXxTc,"0").update();
        return AjaxResult.success(list);
    }

    @ApiOperation("个人账号获取弹窗消息")
//    @PreAuthorize("@loginMemberPermissionService.hasMePermi('business:accountmessage:list')")
    @GetMapping("/tcTdAccList")
    public AjaxResult tcAccountMessage(MlAccountMessage mlAccountMessage) {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        List<MlAccountMessage> list = mlAccountMessageService.lambdaQuery()
                .eq(MlAccountMessage::getTdAcc, tdAcc)
                .eq(MlAccountMessage::getXxTc, "0")
                .eq(MlAccountMessage::getStatus, "0")
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }
        List<MlAccountMessage> mlAccountMessages = list.subList(0, 1);
        // 异步延时5秒后更新
        new Thread(() -> {
            try {
                Thread.sleep(5000);
                for (MlAccountMessage accountMessage : mlAccountMessages) {
                    accountMessage.setXxTc(BigDecimal.ONE);
                }
                this.mlAccountMessageService.updateBatchById(mlAccountMessages);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        // 先返回数据
        return AjaxResult.success(mlAccountMessages);
    }
}
